<?php


namespace common\components;


use common\models\Goods;
use common\models\GoodsStock;
use common\models\Order;
use common\models\OrderGoods;
use common\models\OrderLog;
use common\models\System;
use common\models\User;
use common\models\UserCoupon;
use yii\base\BaseObject;
use yii\queue\JobInterface;
use yii\queue\Queue;

class OrderBehavior extends BaseObject implements JobInterface
{
    const ORDER_DIS = 'dis';
    const ORDER_CANCEL = 'cancel';
    const ORDER_RECEIV = 'receiv';
    public $method;
    public $params;
    public function execute($queue)
    {
        if (!method_exists($this, $this->method)) {
            return false;
        }
        $method = $this->method;
        return $this->$method();
    }

    /**
     * 订单佣金计算
     * @return bool
     */
    public function dis()
    {
        $params = $this->params;
        if (empty($params['order_sn'])||empty($params['userid'])){
            return false;
        }
        $user = User::find()->where(['id'=>$params['userid']])->one();
        if (!$user){
            return false;
        }
        $sys = System::getModel();
        $dis_field = 'trade_price';
        $order = Order::find()
            ->where(['order_sn'=>$params['order_sn']])
            ->select(['id',"$dis_field","total_amount",'one_level','one_brokerage','second_level','second_brokerage','ca_price'])
            ->one();
        if (!$order){
            return false;
        }
        $amount = $order->total_amount-$order->{$dis_field};
        if ($amount<0){
            return false;
        }
        if ($user->one_level){
            $sys->one_level = $sys->one_level??0;
            $one_brokerage = $sys->one_level/100*$amount;
            $order->one_level = $user->one_level;
            $order->one_brokerage = $one_brokerage;
        }
        if ($user->second_level){
            $sys->second_level = $sys->second_level??0;
            $second_brokerage = $sys->second_level/100*$amount;
            $order->second_level = $user->second_level;
            $order->second_brokerage = $second_brokerage;
        }
        if ($user->top_level){
            $sys->top_level = $sys->top_level??0;
            $top_brokerage = $sys->top_level/100*$amount;
            $order->top_level = $user->top_level;
            $order->top_brokerage = $top_brokerage;
        }
        $sys->ca_percentage = $sys->ca_percentage??0;
        if ($sys->ca_percentage>0){
            $order->ca_price = $order->ca_price??0;
            $ca_brokerage = $order->ca_price*($sys->ca_percentage/100);
            $order->ca_brokerage = $ca_brokerage??0;
        }
        if ($order->save()!==false){
            return true;
        }
        return false;
    }

    /**
     * 订单关闭
     * @return bool
     */
    public function cancel()
    {
        $params = $this->params;
        if (empty($params['order_sn'])){
            return false;
        }
        $order = Order::find()->where(['order_sn'=>$params['order_sn']])->one();
        if (!$order){
            return false;
        }
        if ((int)$order->pay_status!==0){
            return false;
        }
        $order->pay_status = 2;
        if ($order->save()){
            if ($order->coupon_id){
                UserCoupon::updateAll(['order_sn'=>'','use'=>0,'use_time'=>''],['id'=>$order->coupon_id]);
            }
            OrderLog::add(4,$params['order_sn'],'','系统','自动关闭订单','订单支付到时，自动关闭订单');
            return true;
        }
        return false;
    }

    /**
     * 订单确认
     * @return bool
     */
    public function receiv()
    {
        $params = $this->params;
        if (empty($params['order_sn'])){
            return false;
        }
        (new Order())->confirmDelivery($params['order_sn'],4,'');
        return true;
    }

    /**
     * 支付成功加销量
     * @return bool
     */
    public function sales()
    {
        $params = $this->params;
        if (empty($params['order_sn'])&&empty($params['order_id'])){
            return false;
        }
        $order_id = $params['order_id']??'';
        if (!$order_id){
            $order_id = Order::find()->where(['order_sn'=>$params['order_sn']])->select(['id'])->scalar();
            if (!$order_id){
                return false;
            }
        }
        $goods = OrderGoods::find()
            ->where(['order_id'=>$order_id])
            ->select(['good_id','spec_id','good_num'])
            ->asArray()->all();
        foreach ($goods as $good){
            Goods::updateAllCounters(['ac_sales'=>$good['good_num']],['id'=>$good['good_id']]);
            GoodsStock::updateAllCounters(['sales'=>$good['good_num']],['good_id'=>$good['good_id'],'spec_id'=>$good['spec_id'],'campus_id'=>$good['campus_id']]);
        }
        return true;
    }
}